package com.doit.day02;

import com.alibaba.fastjson.JSON;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

public class IsNewDemo {
    public static void main(String[] args) {
        //搞一个布隆过滤器对象出来，用于判断guid是否已经出现过
        BloomFilter<Long> bloom = BloomFilter.create(Funnels.longFunnel(), 1000000);

        Properties props = new Properties();
        //必配项
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"group04");
        props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        props.setProperty(ConsumerConfig.ALLOW_AUTO_CREATE_TOPICS_CONFIG,"true");
        props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"true");
        props.setProperty(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"10000");
        //创建消费者对象
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Arrays.asList("event-log"));
        //拉取数据
        while (true){
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(Integer.MAX_VALUE));
            for (ConsumerRecord<String, String> record : records) {
                String jsonStr = record.value();
                //json串转换成对象
                EventLog eventLog = JSON.parseObject(jsonStr, EventLog.class);
                //现在要做的事情是判断这个EventLog对象中的guid有没有出现过把
                boolean flag = bloom.mightContain((long) eventLog.getGuid());
                if (!flag){
                    //设置是否为新用户的值
                    eventLog.setIsNew(1);
                    //如果是一个新用户，将数据标记到布隆过滤器中
                    bloom.put((long) eventLog.getGuid());
                }else {
                    eventLog.setIsNew(0);
                }
                //看看具体是新用户还是老用户
                System.out.println(JSON.toJSONString(eventLog));
            }
        }

    }
}
